<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA


$xml_config = simplexml_load_file('config.xml');

include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/authentification/User.php');

if ((isset($_POST['username'])) && (isset($_POST['email'])) && (isset($_POST['pass1']))){


    $dbserver = $xml_config->database->dbserver;
    $dbname = $xml_config->database->dbname;
    $dbuser = $xml_config->database->dbuser;
    $dbpassword = $xml_config->database->dbpassword;
    $table_prefix = $xml_config->database->table_prefix;

    if((isset($dbserver)) && (isset($dbname)) && (isset($dbuser))){

//       

        if ((isset($_POST['username'])) && (User::check_username_format($_POST['username']))
            && (isset($_POST['email'])) && (User::check_email_format($_POST['email']))
            && (isset($_POST['pass1'])) && (User::check_password_format($_POST['pass1']))){

            User::register_user($_POST['username'], $_POST['pass1'], $_POST['email']);

            $db = new PDO('mysql:host='.$dbserver.';dbname='.$dbname, $dbuser, $dbpassword, array(1002 => 'SET NAMES utf8'));
            $cmd = $db->prepare('UPDATE '.$table_prefix.'_user SET group_id = 3 where id = 1');
            $cmd->execute();

            $content = '<h3>Administrator account created. Install complete</h3>
                <ol>
                    <li>Suppress all files install_stepXXX.php</li>
                    <li>Suppress the file install\sql_scripts\init_db.sql</li>
                    <li>Enjoy Tim\'Roster !!</li>
                </ol>';
        }

    }
    else {
        $content = '<h3>the file config.xml is not well configurate. Please complete the <a href="install_step1.php">first installation step</a></h3>';
    }
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <script type="text/javascript" src="js/login_data_check.js"></script>
        <link rel="shortcut icon" type="images/x-icon" href="./img/quest_jeton.png" />

        <script type="text/javascript">
            function validForm(){
                var error = 0;

                var username = document.getElementById('username').value;
                var email = document.getElementById('email').value;
                var pass1 = document.getElementById('pass1').value;
                var pass2 = document.getElementById('pass2').value;

                //Username must be set
                if (!username){
                    error = 1;
                    alert('Username');
                }

                if ((!email) && (error == 0)){
                    error = 1;
                    alert('Email must be set');
                }

                if (((!pass1) || (!pass2)) && (error == 0)){
                    error = 1;
                    alert('Password must be set');
                }

                //Username's length must be < 32
                if ((!checkUsernameFormat(username)) && (error == 0)){
                    error = 1;
                    alert('lb_username_format;Username\'s length must be between 6 and 32 characters and contains only alphanumeric characters');
                }

                if ((!checkEmailFormat(email)) && (error == 0)){
                    error = 1;
                    alert('Email is not well formated');
                }

                if ((((!checkPasswordFormat(pass1))) || (!checkPasswordFormat(pass2))) && (error == 0)){
                    error = 1;
                    alert('Password\'s length must be between 6 and 32 characters and contains only alphanumeric characters');
                }

                if ((pass1 != pass2) && (error == 0)){
                    error = 1;
                    alert('Passwords do not match');
                }

                //if no error redirect
                if (error == 0){
                    document.register_form.submit();
                }
            }
        </script>
    </head>
    <body>
        <h1>INSTALL STEP 3 - Create first account : </h1>
        <?php
            if (isset($content)) { echo $content; }
        ?>

        <form name ="register_form" method="post" accept-charset="utf-8">
            Login (alphanumerical character, length between 4 and 32) : <br/><input type="text" name="username" id="username" value=""/><br/>
            Email : <br/><input type="text" name="email" id="email" value=""/><br/>
            Password (alphanumerical character, length between 6 and 32) : <br/><input type="password" name="pass1" id="pass1" value=""/><br/>
            Password (again)<br/><input type="password" name="pass2" id="pass2" value=""/><br/>
            <input type="button" value="Register" onclick="validForm();" />
        </form>
    </body>
</html>
